#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct TreeItem TreeItem;
typedef struct TreeLeaf TreeLeaf;

typedef union {
		TreeItem *p_tR_treeLeaf;
		TreeItem *p_tR_treeItem;
}Treeunion;

typedef struct TreeRoot{
	int tR_cursize;
    int tR_valuesize;
    int tR_valuelen;
    char **tR_value;
    int tR_keysize;
    Treeunion **p_tR_uArray;
    //TreeItem **p_tR_treeItems;
}TreeRoot;

struct TreeItem{
	int tI_cursize;
    int tI_valuesize;
    int tI_valuelen;
    char **tI_value;
    int tI_keysize;
	Treeunion **p_tI_uArray;
};


struct TreeLeaf{
	int tL_cursize;
    int tL_valuelen;
    int tL_valuesize;
    char **p_tL_vArray;
};

char* mallocCharMemory(int len)
{
    if (!len)
        return NULL;
    return malloc(sizeof(char)*len);
}

TreeLeaf *createTreeLeaf(int vs, int vl)
{
    if (!vs || !vl)
        return NULL;
    
    //  Create a new TreeLeaf node
    TreeLeaf *_new = malloc(sizeof(TreeLeaf));
    if (!_new)
        return _new;
    
    //  Alloc the memory for value array
    _new->p_tL_vArray = malloc(sizeof(char*)*vs);
    if (!_new->p_tL_vArray) {
        free(_new);
        return NULL;
    }

    //  initialize the value for value member
    for (int i = 0; i < vs; i++) {
        _new->p_tL_vArray[i] = NULL;
    }
    _new->tL_valuelen = vl;
    _new->tL_valuesize = vs;
	_new->tL_cursize = 0;
    return _new;
}

TreeItem *createTreeItem(int vs, int vl, int ks)
{
   if (!vs || !vl || !ks)
        return NULL;
    
    //  
    TreeItem *_new = malloc(sizeof(TreeItem));
    if (!_new)
        return _new;

    //  
    _new->tI_value = malloc(sizeof(char*)*vl);
    if (!_new->tI_value) {
        free(_new);
        return NULL;
    }

    //  When use the value and initailize it
    /*
    for (int i = 0; i < vl; i++) {
        _new->tI_value[i] = malloc(sizeof(char)*vs);
        if (!_new->tI_value[i]) {
            for (int j = 0; j < i; j++) {
                free(_new->tI_value[j]);
            }
            free(_new);
            return NULL;
        }
    }
    */

    _new->p_tI_uArray = malloc(sizeof(Treeunion*)*ks);
    if (!_new->p_tI_uArray) {
        for (int i = 0; i < vl; i++) {
            free(_new->tI_value[i]);
        }
        free(_new);
        return NULL;
    }

	_new->tI_keysize = ks;
	_new->tI_valuelen = vl;
	_new->tI_valuesize = vs;
	_new->tI_cursize = 0;
    return _new;    
}

TreeRoot *createTreeRoot(int vs, int vl, int ks)
{
    if (!vs || !vl || !ks)
        return NULL;

    //
    TreeRoot *_new = malloc(sizeof(TreeRoot));
    if (!_new)
        return _new;

    //
    _new->tR_value = malloc(sizeof(char*)*vl);
    if (!_new->tR_value) {
        free(_new);
        return NULL;
    }

    //
    /*
    for (int i = 0; i < vl; i++) {
        _new->tR_value[i] = malloc(sizeof(char)*vs);
        if (!_new->tR_value[i]) {
            for (int j = 0; j < i; j++) {
                free(_new->tR_value[j]);
            }
            free(_new);
            return NULL;
        }
    }
    */

    //
    _new->p_tR_uArray = malloc(sizeof(Treeunion*)*ks);
    if (!_new->p_tR_uArray) {
        for (int i = 0; i < vl; i++) {
            free(_new->tR_value[i]);
        }
        free(_new);
        return NULL;
    }
	
	_new->tR_cursize = 0;
	_new->tR_keysize = ks;
	_new->tR_valuelen = vl;
	_new->tR_valuesize = vs;

    return _new;
}

int main(int argc, const char *argv[])
{
    TreeRoot *m_TR = createTreeRoot(10,10,10);
    printf("%d,%d,%d\n", m_TR->tR_cursize, m_TR->tR_keysize, m_TR->tR_valuelen);
    getchar();
    return 0;
}
